
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TF
 */
public class FileCache {
    FileCache(){ init(); }
    private HashMap<String,Properties> content;
    private String line;
    private String[] ItemArray;
    private NameMap NameList;
    private int index;
    private Vector<String> raw;
    private void init(){
        content = new HashMap<String,Properties>();
        line = new String();
        ItemArray = new String[3];
        NameList = new NameMap();
        raw = new Vector<String>();
        index = 0;
    }
    public void loadFile(String l){
        line = l;
        raw.add(line);        
        if(line.contains("=") && !line.startsWith("*") && !line.startsWith("#")){
            ItemArray = resolve(line);
            if(ItemArray[0]==null) return;
            content.put(ItemArray[1], new Properties(ItemArray[0],ItemArray[1],ItemArray[2],index));
            //printItemArray();
        }
        index++;
    }
    private String[] resolve(String s){
        String[] ret = new String[3];
        ret[1] = s.trim().substring(0, s.indexOf("="));
        ret[2] = s.trim().substring(s.indexOf("=")+1);
        ret[0] = NameList.getName(ret[1]);
       // content.put(ret[0], ret[1]);
        return ret;
    }
    public void printItemArray(){
        if(line.contains("=") && !line.startsWith("*") && !line.startsWith("#"))
            System.out.println(ItemArray[0]+"|"+ItemArray[1]+"|"+ItemArray[2]);  
    }
    public HashMap<String,Properties> getHashMap(){
        return content;
    }
    public String getName(String key){
        return content.get(key).name;
    }
    public String getCurrentName(){
        return ItemArray[0];
    }
    public String getCurrentVal(){
        return ItemArray[2];
    }
    public String getCurrentPro(){
        return ItemArray[1];
    }
    public String getRawContent(int i){
        return raw.get(i);
    }
    public Vector<String> updateRawContent(){
        for(Map.Entry<String,Properties> entry : content.entrySet()){
            raw.set(entry.getValue().index, (entry.getKey()+"="+entry.getValue().val));
        }
        return raw;
    }
}
